package com.wman.sheep.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

import com.wman.sheep.R;
import com.wman.sheep.common.imageloder.ImageLoader;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 适配一切RecyclerView.ViewHolder
 */
public class RecyclerHolder extends RecyclerView.ViewHolder {
    private final SparseArray<View> mViews;

    public RecyclerHolder(View itemView) {
        super(itemView);
        //一般不会超过8个吧
        this.mViews = new SparseArray<>(8);
    }

    public SparseArray<View> getAllView() {
        return mViews;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public RecyclerHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public RecyclerHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param bm
     * @return
     */
    public RecyclerHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public RecyclerHolder setUrlImageView(Context context, int viewId, String url) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayImage(context, url, iv, R.drawable.sample_placeholder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public RecyclerHolder setUrlImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayImage(context, url, iv, placeHolder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 圆形图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @return
     */
    public RecyclerHolder setUrlCircleImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayCircleImage(context, url, iv, placeHolder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 圆角图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @return
     */
    public RecyclerHolder setUrlRoundImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayRoundImage(context, url, iv, placeHolder);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 圆角图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @param radius
     * @return
     */
    public RecyclerHolder setUrlRoundImageView(Context context, int viewId, String url, int placeHolder, int radius) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayRoundImage(context, url, iv, placeHolder, radius);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 模糊图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @return
     */
    public RecyclerHolder setUrlBlurImageView(Context context, int viewId, String url, int placeHolder) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayBlurImage(context, url, iv, placeHolder, 10);

        return this;
    }

    /**
     * 给ID为viewId的ImageView异步加载url网络图片 模糊图片
     *
     * @param context
     * @param viewId
     * @param url
     * @param placeHolder
     * @param radius
     * @return
     */
    public RecyclerHolder setUrlBlurImageView(Context context, int viewId, String url, int placeHolder, int radius) {
        ImageView iv = getView(viewId);

        ImageLoader.getInstance().displayBlurImage(context, url, iv, placeHolder, radius);

        return this;
    }

    /**
     * 给ID为viewId的view设置LayoutParams
     *
     * @param
     * @return
     */
    public RecyclerHolder setLayoutParams(int viewId, ViewGroup.LayoutParams lp) {
        View view = getView(viewId);
        view.setLayoutParams(lp);

        return this;
    }

    /**
     * Will set background of a view.
     *
     * @param viewId        The view id.
     * @param backgroundRes A resource to use as a background.
     * @return The BaseViewHolder for chaining.
     */
    public RecyclerHolder setBackgroundRes(int viewId, @DrawableRes int backgroundRes) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    /**
     * Set a view visibility to VISIBLE (true) or GONE (false).
     *
     * @param viewId  The view id.
     * @param visible True for VISIBLE, false for GONE.
     * @return The BaseViewHolder for chaining.
     */
    public RecyclerHolder setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public RecyclerHolder setChecked(@IdRes int viewId, boolean checked) {
        View view = getView(viewId);
        // View unable cast to Checkable
        if (view instanceof Checkable) {
            ((Checkable) view).setChecked(checked);
        }
        return this;
    }

    public RecyclerHolder setOnClickListener(@IdRes int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }
}
